package com.wang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.pojo.ArchivesParams;
import com.wang.pojo.ArticleParam;
import com.wang.pojo.CommentParam;
import com.wang.service.ArticleService;
import com.wang.service.CategoryService;
import com.wang.service.CommentService;
import com.wang.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private CategoryService categoryService;

    //文章列表
    @GetMapping("/list/{index}/{num}")
    public Result articles(@PathVariable("index") int index, @PathVariable("num") int num) {
        Result result = articleService.listArticlePage(index, num);
        return result;
    }


    //最热文章
    @GetMapping("/hots")
    public Result hotsArticle() {
        int limit = 5;
        Result result = articleService.hotsArticle(limit);
        return result;
    }

    //最新文章
    @GetMapping("/new")
    public Result newArticle() {
        int limit = 5;
        Result result = articleService.newArticle(limit);
        return result;
    }

    //文章归档
    @GetMapping("/archives")
    public Result listArchives() {
        Result result = articleService.ListArchives();
        return result;
    }

    @PostMapping("/archives")
    public Result listArticleByArchives(@RequestBody ArchivesParams archivesParams) {
        Result result = articleService.ListArticleByArchives(archivesParams);
        return result;
    }

    //文章详细
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long id) {
        System.out.println(id);
        Result result = articleService.detail(id);
        return result;
    }

//    获取文章评论
    @GetMapping("/comment/{id}")
    public Result commentList(@PathVariable("id") Long id) {
        return commentService.findCommentByArticleId(id);
    }

//  写评论
    @PostMapping("/comment/add")
    public Result addComment(@RequestBody CommentParam commentParam) {
        return commentService.addComment(commentParam);
    }

//    查询所有分类
    @GetMapping("/category")
    public Result categoryList() {
        Result result = categoryService.findAll();
        return result;
    }

//    添加文章
    @PostMapping()
    public Result addArticle(@RequestBody ArticleParam articleParam) {
        Result result = articleService.addArticle(articleParam);
        return result;
    }
}
